[POJ-3264] Balanced Lineup


注:数据改编自原题,输入输出略有不同


题目

题目描述

For the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

输入

Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

输出

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

样例输入

1
2
3
4
5
6
7
8
9
10
6 3
1
7
3
4
2
5
1 5
4 6
2 2

样例输出

1
2
3
6
3
0

题解

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAXN 50001
int n,q;
int stmin[MAXN][20];
int stmax[MAXN][20];

void pre_work(){
for(int j = 1; j < 20; ++j)
for(int i = 1; i <= n; ++i)
if(i + (1 << j) - 1 <= n){
stmax[i][j] = max(stmax[i][j-1], stmax[i + (1 << (j - 1))][j - 1]);
stmin[i][j] = min(stmin[i][j-1], stmin[i + (1 << (j - 1))][j - 1]);
}
}

int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d", &stmin[i][0]);
stmax[i][0]=stmin[i][0];
}
pre_work();
for(int i=1;i<=q;i++){
int x,y;
scanf("%d%d",&x,&y);
int k=(int)(log(y - x + 1.0) / log(2.0));
int maxsum = max(stmax[x][k], stmax[y - (1 << k) + 1][k]);
int minsum = min(stmin[x][k], stmin[y - (1 << k) + 1][k]);
printf("%d\n",maxsum-minsum);
}
return 0;
}

线段树

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<cstdio>
using namespace std;
#define MAXN 50001
int n,q;
struct Ttree
{
int maxdata,mindata;
}t[MAXN*4],tmp0;

void push_up(int l,int r,int k){
t[k].maxdata=max(t[k*2].maxdata,t[k*2+1].maxdata);
t[k].mindata=min(t[k*2].mindata,t[k*2+1].mindata);
}

void make(int l,int r,int k){
if(l==r){
scanf("%d",&t[k].maxdata);
t[k].mindata=t[k].maxdata;
return;
}
int mid=(l+r)/2;
make(l,mid,k*2);
make(mid+1,r,k*2+1);
push_up(l,r,k);
return;
}

Ttree ask(int L,int R,int l,int r,int k){
if(L<=l&&r<=R)return t[k];
int mid=(l+r)/2;
Ttree res1;
if(L<=mid)res1=ask(L,R,l,mid,k*2);
else res1=tmp0;
Ttree res2;
if(R>mid)res2=ask(L,R,mid+1,r,k*2+1);
else res2=tmp0;
Ttree res={0};
res.maxdata=max(res1.maxdata,res2.maxdata);
res.mindata=min(res1.mindata,res2.mindata);
return res;
}

int main(){
tmp0.maxdata=-1e9,tmp0.mindata=1e9;
scanf("%d%d",&n,&q);
make(1,n,1);
for(int i=1;i<=q;i++){
int x,y;
scanf("%d%d",&x,&y);
Ttree ans;
ans=ask(x,y,1,n,1);
printf("%d\n",ans.maxdata-ans.mindata);
}
return 0;
}